package com.rdzn.mall.base.service.impl;

import com.rdzn.mall.base.service.IBaseApi;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.MinioUtil;
import org.jeecg.common.util.oss.OssBootUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Slf4j
@Service
public class BaseApiImpl implements IBaseApi {

    @Override
    public String upload(MultipartFile file, String bizPath, String uploadType) {
        String url = "";
        if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
            url = MinioUtil.upload(file,bizPath);
        }else{
            url = OssBootUtil.upload(file,bizPath);
        }
        return url;
    }

    @Override
    public String upload(MultipartFile file, String bizPath, String uploadType, String customBucket) {
        String url = "";
        if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
            url = MinioUtil.upload(file,bizPath,customBucket);
        }else{
            url = OssBootUtil.upload(file,bizPath,customBucket);
        }
        return url;
    }

    @Override
    public void viewAndDownload(String filePath, String uploadpath, String uploadType, HttpServletResponse response) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            if(filePath.startsWith("http")){
                String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
                if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
                    String bucketName = filePath.replace(MinioUtil.getMinioUrl(),"").split("/")[0];
                    String objectName = filePath.replace(MinioUtil.getMinioUrl()+bucketName,"");
                    inputStream = MinioUtil.getMinioFile(bucketName,objectName);
                    if(inputStream == null){
                        bucketName = CommonConstant.UPLOAD_CUSTOM_BUCKET;
                        objectName = filePath.replace(OssBootUtil.getStaticDomain()+"/","");
                        inputStream = OssBootUtil.getOssFile(objectName,bucketName);
                    }
                }else{
                    String bucketName = CommonConstant.UPLOAD_CUSTOM_BUCKET;
                    String objectName = filePath.replace(OssBootUtil.getStaticDomain()+"/","");
                    inputStream = OssBootUtil.getOssFile(objectName,bucketName);
                    if(inputStream == null){
                        bucketName = filePath.replace(MinioUtil.getMinioUrl(),"").split("/")[0];
                        objectName = filePath.replace(MinioUtil.getMinioUrl()+bucketName,"");
                        inputStream = MinioUtil.getMinioFile(bucketName,objectName);
                    }
                }
                response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
            }else{
                // 本地文件处理
                filePath = filePath.replace("..", "");
                if (filePath.endsWith(",")) {
                    filePath = filePath.substring(0, filePath.length() - 1);
                }
                String fullPath = uploadpath + File.separator + filePath;
                String downloadFilePath = uploadpath + File.separator + fullPath;
                File file = new File(downloadFilePath);
                inputStream = new BufferedInputStream(new FileInputStream(fullPath));
                response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
            }
            response.setContentType("application/force-download");// 设置强制下载不打开
            outputStream = response.getOutputStream();
            if(inputStream != null){
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    outputStream.write(buf, 0, len);
                }
                response.flushBuffer();
            }
        } catch (IOException e) {
            response.setStatus(404);
            log.error("预览文件失败" + e.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
}
